package com.example.sunsg.okhttptest.util;

import android.net.Uri;
import android.text.TextUtils;

import com.example.sunsg.okhttptest.bean.LocalImageRequestBuilder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by sunsg on 15/10/12.
 */
public class FrescoImageManager {
    private static final String LOCAL_SCHEME = "file://";

    /**
     * 获取本地图片Uri
     *
     * @param filePath
     *            文件路径
     * @return
     */
    public static Uri getLocalUri(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            if (!filePath.startsWith(LOCAL_SCHEME)) {
                filePath = new StringBuilder().append(LOCAL_SCHEME)
                        .append(filePath).toString();
            }
            return Uri.parse(filePath);
        }
        return null;
    }

    /**
     * 获取网络图片Uri
     * @param url
     * @return
     */

    public static Uri getNetUri(String url) {
        if (!TextUtils.isEmpty(url)) {
            return Uri.parse(url);
        }
        return null;
    }

    /**
     * 获取资源文件图片uri
     * @param packageName
     * @param resId
     * @return
     */
    public static Uri getResUri(String packageName,int resId){
        return Uri.parse("res://"+packageName+"/"+resId);
    }

    /***
     * 加载网络图片没有默认图
     * @param view
     * @param uri
     * @param width
     * @param height
     * @param rotate
     */
    public static void loadNetImage(SimpleDraweeView view, Uri uri, int width, int height,boolean rotate) {
        if(width > 0 && height > 0) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(width, height))
                    .setAutoRotateEnabled(rotate)
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(view.getController())
                    .setImageRequest(request)
                    .build();
            view.setController(controller);
        }
    }

    /**
     * 加载本地图片，没有默认图,兼容encodeUrl到文件名
     * @param view
     * @param uri
     * @param width
     * @param height
     * @param rotate
     */
    public static void loadLocalImage(SimpleDraweeView view, Uri uri, int width, int height,boolean rotate) {
        if(width > 0 && height > 0) {
            ImageRequest request = LocalImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(width, height))
                    .setAutoRotateEnabled(rotate)
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(view.getController())
                    .setImageRequest(request)
                    .build();
            view.setController(controller);
        }
    }


}
